<template>
  <div>
    <base-list ref="auditList"
               :condition="condition"
               :datas.sync="auditData"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="7"
                  class="search-width4">
            <el-form-item label="货主单位">
              <auto-search-name v-model="condition['search_EQ_cargoOwnerId']"
                                placeholder="请选择"
                                @selectId="v=>condition['search_EQ_cargoOwnerId']=v"
                                :name="form.cargoOwnerName"></auto-search-name>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="search-width4">
            <el-form-item label="控制单位">
              <auto-search-org v-model="condition['search_EQ_controlOrgName']"
                               placeholder="请选择"
                               @selectName="v=>condition['search_EQ_controlOrgName']=v"
                               :allOrg="true"
                               :name="form.controlOrgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="search-width4">
            <el-form-item label="状态">
              <el-select v-model="condition['search_EQ_auditStatus']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictAuditStatus"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ml-80"
                       @click="loadData">查 询</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table slot="list"
                :data="auditData"
                border
                stripe
                max-height="500">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主单位"
                         min-width="200"></el-table-column>
        <el-table-column prop="controlOrgName"
                         label="控制单位"
                         min-width="200"></el-table-column>
        <el-table-column prop="auditStatus"
                         label="状态"
                         min-width="200">
          <template slot-scope="scope">
            {{ scope.row.auditStatus | _filterAuditStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="controlType"
                         label="控制类型"
                         min-width="200">
          <template slot-scope="scope">
            {{ scope.row.controlType | _filterControlType }}
          </template>
        </el-table-column>
        <el-table-column prop="controlWay"
                         label="控制方式"
                         min-width="150">
          <template slot-scope="scope">
            {{ scope.row.controlWay | _filterControlWay }}
          </template>
        </el-table-column>
        <el-table-column prop="controlCode"
                         label="控制代码"
                         min-width="120"></el-table-column>
        <el-table-column prop="releasedWeight"
                         label="放货量"
                         min-width="120"></el-table-column>
        <el-table-column prop="pledgedWeight"
                         label="质押量"
                         min-width="120"></el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
                         width="240">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       :disabled="scope.row.auditStatus==='C'"
                       @click="handleDialogFormOpen(true,scope.row)">
              编辑
            </el-button>
            <el-button type="text"
                       class="danger"
                       size="small"
                       :disabled="scope.row.auditStatus==='C'"
                       @click="handleRemove(scope.row)">删除
            </el-button>
            <el-button size="small"
                       type="text"
                       @click="handleViewDialogForm(scope.row)">
              查看
            </el-button>
            <el-button size="small"
                       type="text"
                       :disabled="scope.row.auditStatus==='C' || scope.row.auditStatus==='D'"
                       @click="handleAuditDialogForm(scope.row)">
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 @click="handleRouterPush({path:'/goods/things',name:'货权管理'})">返回</el-button>
    </div>
    <add-property-control-form ref="addPropertyControlForm"
                               :id="id"
                               @load="loadData"></add-property-control-form>
    <audit-property-control-form ref="auditPropertyControlForm"
                                 :id="id"
                                 @load="loadData"></audit-property-control-form>
  </div>
</template>

<script>
import { detailAudit, deleteAudit } from '@/api/things/auditList' //详情
import { getAuditList } from '@/api/things/auditList' //审核统计
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchName from '@/components/autocomplete/autoSearchName'
import addPropertyControlForm from './addPropertyControlForm' //编辑货权控制
import auditPropertyControlForm from './auditPropertyControlForm' //审核、查看货权控制

export default {
  name: 'auditList',
  components: {
    autoSearchOrg,
    autoSearchName,
    addPropertyControlForm,
    auditPropertyControlForm
  },
  data() {
    return {
      form: this.clearForm(),
      disabled: false,
      condition: {}, // 查询条件
      auditData: [], // table数组
      pageSearch: getAuditList, // 分页查询api接口
      id: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.auditList.loadData()
    },
    // 编辑
    handleDialogFormOpen(bool, item) {
      if (bool) {
        detailAudit(item.id).then(res => {
          this.id = item.id
          console.log(res.data)
          //判断是否显示总质押量和总放货量
          if (
            res.data.totalPledgedWeight === 0 &&
            res.data.totalReleasedWeight === 0
          ) {
            this.$refs.addPropertyControlForm.disabled = false
            this.$refs.addPropertyControlForm.show = false
          } else {
            this.$refs.addPropertyControlForm.disabled = true
            this.$refs.addPropertyControlForm.show = true
          }
          this.$refs.addPropertyControlForm.form = JSON.parse(
            JSON.stringify(res.data)
          )
        })
        //判断显示放货或者质押输入框
        if (item.controlType === 'FH') {
          this.$refs.addPropertyControlForm.zy_disabled = true
          this.$refs.addPropertyControlForm.fh_disabled = false
        } else {
          this.$refs.addPropertyControlForm.fh_disabled = true
          this.$refs.addPropertyControlForm.zy_disabled = false
        }
        this.$refs.addPropertyControlForm.isEdit = true
        this.$refs.addPropertyControlForm.dialogForm = true
      } else {
        this.$refs.addPropertyControlForm.dialogForm = true
      }
    },
    //查看
    handleViewDialogForm(item) {
      detailAudit(item.id).then(res => {
        if (
          res.data.totalPledgedWeight === 0 &&
          res.data.totalReleasedWeight === 0
        ) {
          this.$refs.auditPropertyControlForm.show = false
        } else {
          this.$refs.auditPropertyControlForm.show = true
        }
        this.$refs.auditPropertyControlForm.form = JSON.parse(
          JSON.stringify(res.data)
        )
      })
      //审核状态为已退回，查看显示被退回的原因
      if (item.auditStatus === 'D') {
        this.$refs.auditPropertyControlForm.show2 = true
      }
      this.$refs.auditPropertyControlForm.isAudit = false
      this.$refs.auditPropertyControlForm.show3 = false
      this.$refs.auditPropertyControlForm.dialogForm = true
    },
    //审核
    handleAuditDialogForm(item) {
      this.id = item.id
      detailAudit(item.id).then(res => {
        if (
          res.data.totalPledgedWeight === 0 &&
          res.data.totalReleasedWeight === 0
        ) {
          this.$refs.auditPropertyControlForm.show = false
        } else {
          this.$refs.auditPropertyControlForm.show = true
        }
        this.$refs.auditPropertyControlForm.form = JSON.parse(
          JSON.stringify(res.data)
        )
      })
      this.$refs.auditPropertyControlForm.isAudit = true
      this.$refs.auditPropertyControlForm.show = true
      this.$refs.auditPropertyControlForm.show2 = false
      this.$refs.auditPropertyControlForm.show3 = true
      this.$refs.auditPropertyControlForm.dialogForm = true
    },

    // 账户信息
    handleAccountInfoForm() {
      this.$refs.accountInfoForm.dialogForm = true
    },
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteAudit(item.id).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
